package com.vitria.installer.manager;

import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import com.vitria.installer.node.InstallNodeMXBean;

public class InstallManager {

    private static final String OBJ_NAME_STR = "com.vitria.installer:type=InstallNode";

    public static InstallNodeMXBean getInstallNodeMXBean(String host, int port) 
            throws Exception {
        String serviceNameTmpl = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi";
        JMXServiceURL url = new JMXServiceURL(String.format(serviceNameTmpl, host, port));
        JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        ObjectName mxbeanName = new ObjectName(OBJ_NAME_STR);
        return JMX.newMXBeanProxy(mbsc, mxbeanName, InstallNodeMXBean.class);
    }
}
